<template>
  <el-dialog
      title="编辑角色"
      :close-on-click-modal ="false"
      @close="EditDialogClosed"
      :visible.sync="EditDialogVisible"
      width="50%">
      <!-- 内容主体区域 -->
      <el-form :model="FromData" :rules="EditRolesRules" ref="EditRolesRef" >
        <el-form-item label="角色ID">
          <el-input v-model="FromData.roleId" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="FromData.roleName" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input type="textarea" :rows="2" show-word-limit maxlength="250" v-model="FromData.roleDesc" placeholder="请输入角色描述"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="EditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="EditRelos(FromData.roleId)">确 定</el-button>
      </span>
    </el-dialog>
</template>

<script>
export default {
  name: 'VueShopEdirRoles',
  props:['EditRolesFrom'],
  data() {
    return {
      EditDialogVisible:false,
      FromData:{},
      EditRolesRules:{
        roleName:[
          {required: true, message:'输入角色名称', triggt:'blur'},
          {min: 2, max:10, message: "角色名长度在3-10个字符之间", trigger: 'blur'}
        ],
        roleDesc:[
          {required: true, message:'输入角色描述', triggt:'blur'},
        ]
      }
    };
  },
  watch:{
    "EditRolesFrom"(value){
      this.FromData = value
    }
  },
  methods: {
    /**
     * @name EditRelos()
     * @提交角色编辑
     */
    EditRelos(id){
      this.$refs.EditRolesRef.validate(async valied => {
        if (!valied) return;
        const {data:res} = await this.$http.put(`roles/${id}`,{
          ...this.FromData
        })
        console.log(res);
        if(res.meta.status !== 200){
          return this.$message.error('修改角色信息失败');
        }
          this.$message.success('修改角色信息完成');
          this.EditDialogVisible = false;
          this.$emit('getRolesList');
      })
    },
    EditDialogClosed(){
      this.$refs.EditRolesRef.resetFields();
    }
  },
};
</script>

<style lang="less" scoped>

</style>